package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.bo.StorageParam;
import com.cskaoyan.bean.pojo.MallAdmin;
import com.cskaoyan.bean.pojo.MallAdminExample;
import com.cskaoyan.bean.pojo.MallStorage;
import com.cskaoyan.bean.pojo.MallStorageExample;
import com.cskaoyan.mapper.MallStorageMapper;
import com.cskaoyan.service.StorageService;
import com.cskaoyan.util.MyPageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: Fengyi
 * @description: 对象管理service实现类
 * @time: 2021/7/9 19:24
 */
@Service
public class StorageServiceImpl implements StorageService {

    @Autowired
    MallStorageMapper mallStorageMapper;

    @Override
    public BaseData<MallStorage> queryFiles(StorageParam param) {
        MyPageHelper.startPage(param);


        MallStorageExample mallStorageExample  = new MallStorageExample();
        MallStorageExample.Criteria criteria = mallStorageExample.createCriteria();
        //逻辑删除
        criteria.andDeletedEqualTo(false);
        if (param.getName() != null) {
            criteria.andNameLike("%" + param.getName() + "%");
        }
        if (param.getKey() != null) {
            //对象存储，根据key模糊查询 修改 key like  --> `key` like
            criteria.andKeyLike("%" + param.getKey() + "%");
        }
        mallStorageExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<MallStorage> storages = mallStorageMapper.selectByExample(mallStorageExample);

        long total = mallStorageMapper.countByExample(mallStorageExample);

        return BaseData.ok(storages, total);
    }

    @Override
    public void updateStorage(MallStorage mallStorage) {
        mallStorageMapper.updateByPrimaryKeySelective(mallStorage);
    }

    @Override
    public void deleteStorage(MallStorage mallStorage) {
        mallStorageMapper.updateDeleted(mallStorage.getId(), new Date());
    }
}
